
import { useSession } from '@/stores/sesstion';
import { createRouter, createWebHashHistory } from 'vue-router';
import Home from '@/views';
import Login from '@/views/login';
import Update from '@/views/update';
import AddAccount from '@/views/addAccount';
import { cache } from '@/utils/utils';


const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: Home
    }, {
      path: '/login',
      component: Login
    }, {
      path: '/update',
      component: Update
    },
    {
      path: '/account',
      component: AddAccount
    }
  ]
});


router.beforeEach((to, _from, next) => {
  const ismMin = cache.get('__is_main__');
  if (!ismMin) {
    if (to.fullPath != '/account') {
      next('/account');
    } else {
      next();
    }
  } else if (!useSession().isLogin && to.fullPath != '/login' && to.fullPath != '/update') {
    next('/login');
  } else {
    next();
  }
});
// @ts-ignore (define in dts)
window.router = router;

export default router;

